package com.lianniu.web.tea.calculator;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.web.base.BaseResult;

@Controller
@RequestMapping("/rest/tea/calculator")
public class CalculatorController {
	private final static Logger logger = Logger.getLogger(CalculatorController.class);
	
	@RequestMapping(value="/calculator", method = RequestMethod.POST)
	public @ResponseBody BaseResult getOrderInfoBefore(HttpServletRequest req){
		String priceStr = req.getParameter("price");
		Long price = Long.valueOf(priceStr);
		String landType = req.getParameter("landType");
		if ("0".equals(landType)) {
			price = price-20l;
		}
		String teaType = req.getParameter("teaType");
		if ("0".equals(teaType)) {
			price = price+20l;
		}else {
			price = price+50l;
		}
		String teaCategory = req.getParameter("teaCategory");
		if (StringUtils.isNotEmpty(teaCategory)) {
			String[] split = teaCategory.split(",");
			Long length = (long) split.length;
			price = price + length*10l;
		}
		String landArea = req.getParameter("landArea");      //土地面积
		String subleasePeriod = req.getParameter("subleasePeriod");  //转租年限
		String areaProportion = req.getParameter("areaProportion");
		if ("0".equals(areaProportion)) {
			price = price+10l;
		}else {
			price = price-10l;
		}
		String warrantType = req.getParameter("warrantType");
		price = price-2l;
		String limitedTimeStr = req.getParameter("limitedTime");
		Long limitedTime = Long.valueOf(limitedTimeStr);
		if (limitedTime >= 20l) {
			price = price+10l;
		}else {
			price = price-10l;
		}
		String permissionsType = req.getParameter("permissionsType");
		if ("0".equals(permissionsType)) {
			price = price+2l;
		}
		String soilTexture = req.getParameter("soilTexture");
		if (StringUtils.isNotEmpty(soilTexture)) {
			if (soilTexture.contains("0")) {
				price = price-1l;
			}
			if (soilTexture.contains("1")) {
				price = price-2l;
			}
		}
		String soilThickness = req.getParameter("soilThickness");
		if ("0".equals(soilThickness)) {
			price = price+2l;
		}else if ("1".equals(soilThickness)){
			price = price+1l;
		}else if ("3".equals(soilThickness)){
			price = price-1l;
		}else if ("4".equals(soilThickness)){
			price = price-2l;
		}
		String landFertility = req.getParameter("landFertility");
		if ("0".equals(landFertility)) {
			price = price+2l;
		}else if ("1".equals(landFertility)){
			price = price+1l;
		}else if ("3".equals(landFertility)){
			price = price-1l;
		}else if ("4".equals(landFertility)){
			price = price-2l;
		}
		String blockShape = req.getParameter("blockShape");
		if ("0".equals(blockShape)) {
			price = price+2l;
		}
		String flatness = req.getParameter("flatness");
		if ("0".equals(flatness)) {
			price = price+2l;
		}else if ("1".equals(flatness)){
			price = price+2l;
		}else if ("4".equals(flatness)){
			price = price-2l;
		}
		String irrigation = req.getParameter("irrigation");
		if ("0".equals(irrigation)) {
			price = price+2l;
		}else if ("3".equals(irrigation)){
			price = price-2l;
		}
		String drainage = req.getParameter("drainage");
		if ("0".equals(drainage)) {
			price = price+2l;
		}else if ("3".equals(drainage)){
			price = price-2l;
		}
		String waterSupply = req.getParameter("waterSupply");
		if ("0".equals(waterSupply)) {
			price = price+2l;
		}else if ("2".equals(waterSupply)){
			price = price-2l;
		}
		String powerSupply = req.getParameter("powerSupply");
		if ("0".equals(powerSupply)) {
			price = price+2l;
		}else if ("2".equals(powerSupply)){
			price = price-2l;
		}
		String network = req.getParameter("network");
		if ("0".equals(network)) {
			price = price+2l;
		}else if ("2".equals(network)){
			price = price-2l;
		}
		String enterRoad = req.getParameter("enterRoad");
		if ("1".equals(network)) {
			price = price-2l;
		}else if ("2".equals(network)){
			price = price+2l;
		}
		String mechanization = req.getParameter("mechanization");
		if ("3".equals(mechanization)) {
			price = price-2l;
		}
		String subsidiary = req.getParameter("subsidiary");
		if (StringUtils.isNotEmpty(subsidiary)) {
			String[] split = subsidiary.split(",");
			Long length = (long) split.length;
			price = price + length*2l;
		}
		String complement = req.getParameter("complement");
		if (StringUtils.isNotEmpty(complement)) {
			String[] split = complement.split(",");
			Long length = (long) split.length;
			price = price + length*2l;
		}
		String airport = req.getParameter("airport");
		if ("0".equals(airport)) {
			price = price-1l;
		}else if ("1".equals(airport)){
			price = price+2l;
		}else if ("2".equals(airport)){
			price = price+1l;
		}else if ("3".equals(airport)){
			price = price-1l;
		}
		String highRail = req.getParameter("highRail");
		if ("0".equals(highRail)) {
			price = price+1l;
		}else if ("2".equals(highRail)){
			price = price-1l;
		}else if ("3".equals(highRail)){
			price = price-2l;
		}
		String highSpeed = req.getParameter("highSpeed");
		if ("0".equals(highSpeed)) {
			price = price+1l;
		}else if ("2".equals(highSpeed)){
			price = price-1l;
		}else if ("3".equals(highSpeed)){
			price = price+2l;
		}
		String railway = req.getParameter("railway");
		if ("0".equals(railway)) {
			price = price+1l;
		}else if ("2".equals(railway)){
			price = price-1l;
		}else if ("3".equals(railway)){
			price = price-2l;
		}
		String highway = req.getParameter("highway");
		if ("0".equals(highway)) {
			price = price+1l;
		}else if ("2".equals(highway)){
			price = price-1l;
		}else if ("3".equals(highway)){
			price = price-2l;
		}
		String port = req.getParameter("port");
		if ("0".equals(port)) {
			price = price+1l;
		}else if ("2".equals(port)){
			price = price-1l;
		}else if ("3".equals(port)){
			price = price-2l;
		}
		String landUseStatus = req.getParameter("landUseStatus");
		if (StringUtils.isNotEmpty(landUseStatus)) {
			if (landUseStatus.contains("0")) {
				price = price+1l;
			}
			if (landUseStatus.contains("1")) {
				price = price+2l;
			}
		}
		String whatManager = req.getParameter("whatManager");
		if (StringUtils.isNotEmpty(whatManager)) {
			String[] split = whatManager.split(",");
			Long length = (long) split.length;
			price = price+length;
		}
		String income = req.getParameter("income");
		if (StringUtils.isNotEmpty(income)) {
			if (income.contains("1")) {
				price = price-2l;
			}
		}
		String laborResources = req.getParameter("laborResources");
		if ("0".equals(laborResources)) {
			price = price+2l;
		}else if ("2".equals(laborResources)){
			price = price-2l;
		}
		String capitaIncome = req.getParameter("capitaIncome");
		if ("1".equals(capitaIncome)) {
			price = price+2l;
		}else if ("2".equals(capitaIncome)){
			price = price+5l;
		}else if ("3".equals(capitaIncome)){
			price = price+8l;
		}else if ("4".equals(capitaIncome)){
			price = price+10l;
		}
		String subsidy = req.getParameter("subsidy");
		if (StringUtils.isNotEmpty(subsidy)) {
			String[] split = subsidy.split(",");
			Long length = (long) split.length;
			if (length > 1) {
				price = price+5l;
			}
		}
		String policy = req.getParameter("policy");
		if (StringUtils.isNotEmpty(policy)) {
			String[] split = policy.split(",");
			Long length = (long) split.length;
			if (length > 1) {
				price = price+10l;
			}
		}
		
		Long total = price * Long.valueOf(landArea) * Long.valueOf(subleasePeriod);
		
		return BaseResult.success(total);
	}
}
